import { Snake } from '../entities/Snake';
import { Food } from '../entities/Food';
import { Vector2D } from '../utils/Vector2D';

export class CollisionSystem {
    public checkCollision(point: Vector2D, food: Food, rangeMultiplier: number = 1): boolean {
        // 增加基础判定范围并根据食物类型调整
        const baseRadius = 20;
        let collisionRadius = baseRadius * rangeMultiplier;
        
        // 根据食物类型调整碰撞半径
        switch(food.getType()) {
            case 'energy':
                collisionRadius *= 1.2; // 普通食物稍微增加判定范围
                break;
            case 'poison':
                collisionRadius *= 1.5; // 毒药食物判定范围更大
                break;
            case 'magnet':
                collisionRadius *= 1.8; // 磁铁食物判定范围最大
                break;
        }

        return point.distance(food.getPosition()) < collisionRadius;
    }

    public checkBoundaryCollision(head: Vector2D, canvas: HTMLCanvasElement): boolean {
        const margin = 10; // 边界碰撞余量
        return (
            head.x < margin ||
            head.x > canvas.width - margin ||
            head.y < margin ||
            head.y > canvas.height - margin
        );
    }

    public checkSelfCollision(snake: Snake): boolean {
        const head = snake.getHead();
        const segments = snake.getSegments();
        
        // 从第4节开始检查（允许转弯时短暂的自身接触）
        for (let i = 4; i < segments.length; i++) {
            const segment = segments[i];
            if (head.distance(segment) < 10) {
                return true;
            }
        }
        return false;
    }

    public checkMagnetRange(point: Vector2D, food: Food, range: number): boolean {
        return point.distance(food.getPosition()) < range;
    }
}